import axios from "axios";

//加解密
import { encryptCBC, decryptCBC } from "../crypt/index"
//axios
export const networkRequest = axios;

//baseURL
export const development = 'http://172.26.2.98:8081';

//密钥
const secretKey = 'kokL8G4RmNfV69cdcJ9dfnrlFGsLy6mH';


//基础配置
export const baseRequestConfig = {
    baseURL: development,
    url: '***',
    data: '***',
    method: 'post',
    timeout: 1000 * 30,
    headers: {
        'Content-Type': 'text/plain;charset=UTF-8',
    },
    transformRequest: [function (data, headers) {
        //请求头增加参数
        //headers.sessionId = '这是参数';
        //1、map转json对象
        const str = Object.fromEntries(data);
        //2、json象转字符串
        const str1 = JSON.stringify(str);
        //3、字符串加密
        const ciphertext = encryptCBC(str1, secretKey);
        return ciphertext;
    }],
    transformResponse: [function (data) {
        // 对响应的data进行处理
        try {
            //1、解密数据
            const str = decryptCBC(data, secretKey);
            //2、json字符串转对象
            const respData = JSON.parse(str);
            return respData;
        } catch (error) {
            // 解密失败
            return error.message;
        }
    }],
}
